﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using AssetManageSystem.Model.OrgManage;


namespace AssetManageSystem.ViewModel.OrgManage.DepartmentVMs
{
    public partial class DepartmentListVM : BasePagedListVM<Department_View, DepartmentSearcher>
    {
        public List<TreeSelectListItem> AllParents { get; set; }
        protected override void InitVM()
        {
            AllParents = DC.Set<Department>().GetTreeSelectListItems(Wtm, x => x.DepName);
        }

        protected override List<GridAction> InitGridAction()
        {
            return new List<GridAction>
            {
                this.MakeStandardAction("Department", GridActionStandardTypesEnum.Create, Localizer["Sys.Create"],"OrgManage", dialogWidth: 800),
                this.MakeStandardAction("Department", GridActionStandardTypesEnum.Edit, Localizer["Sys.Edit"], "OrgManage", dialogWidth: 800),
                this.MakeStandardAction("Department", GridActionStandardTypesEnum.Delete, Localizer["Sys.Delete"], "OrgManage", dialogWidth: 800),
                this.MakeStandardAction("Department", GridActionStandardTypesEnum.Details, Localizer["Sys.Details"], "OrgManage", dialogWidth: 800),
                this.MakeStandardAction("Department", GridActionStandardTypesEnum.BatchEdit, Localizer["Sys.BatchEdit"], "OrgManage", dialogWidth: 800),
                this.MakeStandardAction("Department", GridActionStandardTypesEnum.BatchDelete, Localizer["Sys.BatchDelete"], "OrgManage", dialogWidth: 800),
                this.MakeStandardAction("Department", GridActionStandardTypesEnum.Import, Localizer["Sys.Import"], "OrgManage", dialogWidth: 800),
                this.MakeStandardAction("Department", GridActionStandardTypesEnum.ExportExcel, Localizer["Sys.Export"], "OrgManage"),
            };
        }

        protected override IEnumerable<IGridColumn<Department_View>> InitGridHeader()
        {
            return new List<GridColumn<Department_View>>{
                this.MakeGridHeader(x => x.DepName),
                this.MakeGridHeader(x => x.DisplayOrder),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<Department_View> GetSearchQuery()
        {
            var query = DC.Set<Department>()
                .CheckContain(Searcher.DepName, x => x.DepName)
                .CheckEqual(Searcher.ParentId, x => x.ParentId).ToList();
            var topdata = query.Where(x => x.ParentId == null).ToList().FlatTree(x => x.DisplayOrder).ToList();
            topdata.ForEach((x) => { int l = x.GetLevel(); for (int i = 0; i < l; i++) { x.DepName = "&nbsp;&nbsp;&nbsp;&nbsp;" + x.DepName; } });
            int order = 0;
            var data = topdata.Select(x => new Department_View
            {
                ID = x.ID,
                DepName = x.DepName,
                DisplayOrder = x.DisplayOrder,
                Children = x.Children?.Select(y => new Department_View
                {
                    ID = y.ID,
                }),
                ExtraOrder = order++,
                ParentID = x.ParentId,
                Parent = x.Parent,
                HasChild = (x.Children != null && x.Children.Count() > 0) ? true : false,
            })
            .OrderBy(x => x.ExtraOrder);
            return data.AsQueryable() as IOrderedQueryable<Department_View>;
        }
    }

    public class Department_View : Department
    {
        [Display(Name = "排序")]
        public new int? DisplayOrder { get; set; }
        public bool HasChild { get; set; }
        public new IEnumerable<Department_View> Children { get; set; }
        public new Department Parent { get; set; }
        public Guid? ParentID { get; set; }
        public int ExtraOrder { get; set; }
    }
}
